from typing import Dict, Any
from .data_agent import DataCollectionAgent
from .summary_agent import SummaryGenerationAgent
from .copywriting_agent import CopywritingGenerationAgent
from .trend_analysis_agent import TrendAnalysisAgent
from .publish_agent import PublishAgent
import os

class AgentFactory:
    """智能体工厂"""
    
    @staticmethod
    def create_agent(agent_type: str, config: Dict[str, Any] = None) -> Any:
        """创建智能体实例"""
        config = config or {}
        
        if agent_type == "DataCollectionAgent":
            return DataCollectionAgent(config)
        elif agent_type == "SummaryGenerationAgent":
            # 构建默认配置文件路径
            config_path = config.get("config_path")
            if not config_path:
                config_path = os.path.join(os.path.dirname(__file__), 'config.json')
            return SummaryGenerationAgent(config_path)
        elif agent_type == "CopywritingGenerationAgent":
            # 构建默认配置文件路径
            config_path = config.get("config_path")
            if not config_path:
                config_path = os.path.join(os.path.dirname(__file__), 'config.json')
            return CopywritingGenerationAgent(config_path)
        elif agent_type == "TrendAnalysisAgent":
            # 构建默认配置文件路径
            config_path = config.get("config_path")
            if not config_path:
                config_path = os.path.join(os.path.dirname(__file__), 'config.json')
            return TrendAnalysisAgent(config_path)
        elif agent_type == "PublishAgent":
            # 构建默认配置文件路径
            config_path = config.get("config_path")
            if not config_path:
                config_path = os.path.join(os.path.dirname(__file__), 'config.json')
            return PublishAgent(config_path)
        else:
            raise ValueError(f"未知的智能体类型: {agent_type}")
    
    @staticmethod
    def create_all_agents(config_path: str = None) -> Dict[str, Any]:
        """创建所有智能体"""
        agents = {}
        config = {"config_path": config_path} if config_path else {}
        
        agents["DataCollectionAgent"] = AgentFactory.create_agent("DataCollectionAgent", config)
        agents["SummaryGenerationAgent"] = AgentFactory.create_agent("SummaryGenerationAgent", config)
        agents["CopywritingGenerationAgent"] = AgentFactory.create_agent("CopywritingGenerationAgent", config)
        agents["TrendAnalysisAgent"] = AgentFactory.create_agent("TrendAnalysisAgent", config)
        agents["PublishAgent"] = AgentFactory.create_agent("PublishAgent", config)
        
        return agents
    
    @staticmethod
    def get_available_agent_types() -> list:
        """获取可用的智能体类型"""
        return [
            "DataCollectionAgent",
            "SummaryGenerationAgent", 
            "CopywritingGenerationAgent",
            "TrendAnalysisAgent",
            "PublishAgent"
        ]
